package tom.spring.cloud.provider;

import ch.qos.logback.classic.helpers.MDCInsertingServletFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;

import java.util.Arrays;

/**
 * 开启debug日志 web: debug
 * 可以看到日志如：DEBUG [provider02-sleuth,64dbbdfbd83496f5,64dbbdfbd83496f5,false] 185284 --- [io-8021-exec-10] m.m.a.RequestResponseBodyMethodProcessor : Writing ["hello, sleuth!"]
 *
 * [provider02-sleuth,64dbbdfbd83496f5,64dbbdfbd83496f5,false]
 * [服务名，traceId, spanId, 是否收集存储]
 *
 * Sleuth结合Zipkin采用Servlet Filter将trace信息存储在request的attributes中，
 * 存储对象为TraceContext, 采用slf4j的MDC模式实现 Mapped Diagnostic Context.
 * {@link ch.qos.logback.classic.helpers.MDCInsertingServletFilter}
 * @author ZHUFEIFEI
 */
@EnableDiscoveryClient
@SpringBootApplication
public class Provider02SleuthApplication {
    public static void main(String[] args) {
        SpringApplication.run(Provider02SleuthApplication.class, args);
    }

    /**
     * 注册logback实现的MDC Filter，增强型获取请求更多信息
     * @return
     */
    @Bean
    public FilterRegistrationBean mdcRegistrationBean() {
        FilterRegistrationBean<MDCInsertingServletFilter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new MDCInsertingServletFilter());
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }
}
